package com.xuzimian.demo.aws.cloud.s3;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.InstanceProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3ObjectSummary;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;

public class AmazonS3Demo {

    public static void main(String[] args) throws URISyntaxException {
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withRegion("us-east-2")
                .withCredentials(new InstanceProfileCredentialsProvider(false))
                .build();

        for (S3ObjectSummary objectSummary : s3.listObjects("swgen-test").getObjectSummaries()) {
            System.out.println(objectSummary);
        }
    }

    private static PutObjectResult testUploadFile(AmazonS3 s3, String buketName, String key, File file) {
        try {
            return s3.putObject(buketName, key, file);
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
            throw e;
        }
    }

    private static void testListBuckets(AmazonS3 s3) {
        List<Bucket> buckets = s3.listBuckets();
        System.out.println("Your Amazon S3 buckets are:");
        for (Bucket b : buckets) {
            System.out.println("* " + b.getName());
        }
    }

}
